package com.newcoin.xgb.config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {


    public XssHttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    public String[] getParameterValues(String parameter) {
        String[] values = super.getParameterValues(parameter);
        if (values == null) {
            return null;
        } else {
            int count = values.length;
            String[] encodedValues = new String[count];
            for (int i = 0; i < count; ++i) {
                encodedValues[i] = cleanXSS(values[i]);
            }
            return encodedValues;
        }
    }

    public String getParameter(String parameter) {
        String value = super.getParameter(parameter);
        return value == null ? null : cleanXSS(value);
    }

    public String getHeader(String name) {
        String value = super.getHeader(name);
        return value == null ? null : cleanXSS(value);
    }


    public String cleanXSS(String value) {
        value = value.replaceAll("<", "& lt;").replaceAll(">", "& gt;");
        value = value.replaceAll("\\(", "& #40;").replaceAll("\\)", "& #41;");
        value = value.replaceAll("'", "& #39;");
        value = value.replaceAll("eval\\((.*)\\)", "");
        value = value.replaceAll("[\\\"\\'][\\s]*javascript:(.*)[\\\"\\']", "\"\"");
        value = value.replaceAll("script", "");
        value = value.replaceAll("\\{", "【").replaceAll("\\}", "】");
        value = value.replaceAll("&", "请你做个人吧");
        value = value.replaceAll("INSERT", "请你做个人吧");
        value = value.replaceAll("insert", "请你做个人吧");
        value = value.replaceAll("UPDATE", "请你做个人吧");
        value = value.replaceAll("update", "请你做个人吧");
        value = value.replaceAll("DELETE", "请你做个人吧");
        value = value.replaceAll("delete", "请你做个人吧");
        return value;
    }
}
